package com.scgk.sync.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.scgk.sync.common.constant.SFTPConstants;
import com.scgk.sync.common.utils.SftpClientUtil;
import com.scgk.sync.doamin.ScgkCgRkldYdXzqczrk;
import com.scgk.sync.doamin.ScgkCgRkldYdXzqdqrk;
import com.scgk.sync.mapper.ScgkCgRkldYdXzqczrkMapper;
import com.scgk.sync.service.IScgkCgRkldYdXzqczrkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 行政区常驻人口/月粒度 服务实现类
 * </p>
 *
 * @author ws
 * @since 2024-01-21
 */
@Service
public class ScgkCgRkldYdXzqczrkServiceImpl extends ServiceImpl<ScgkCgRkldYdXzqczrkMapper, ScgkCgRkldYdXzqczrk> implements IScgkCgRkldYdXzqczrkService {

    @Autowired
    private ScgkCgRkldYdXzqczrkMapper xzqczrkMapper;

    @Override
    public void addXzqczrk(List<String> list) {

        if (CollectionUtil.isNotEmpty(list)) {
            list.stream().forEach(item -> {
                String[] split = item.split("\\$");

                ScgkCgRkldYdXzqczrk build = ScgkCgRkldYdXzqczrk.builder()
                        .citycode(split[0])
                        .countycode(split[1])
                        .dqrk(Integer.valueOf(split[2]))
                        .month(DateUtil.format(DateUtil.parse(split[3], "yyyyMM"), "yyyy-MM"))
                        .build();


                Integer selectCount = xzqczrkMapper.selectCount(new LambdaQueryWrapper<ScgkCgRkldYdXzqczrk>()
                        .eq(ScgkCgRkldYdXzqczrk::getCitycode, build.getCitycode())
                        .eq(ScgkCgRkldYdXzqczrk::getCountycode, build.getCountycode())
                        .eq(ScgkCgRkldYdXzqczrk::getMonth, build.getMonth()));
                if(selectCount == 0){
                    xzqczrkMapper.insert(build);
                }

            });
        }



    }

    /**
     * 获取 FolderName {当天时间} 目录下 的 fileName {行政区常驻人口 dwa_ovm_ep_city_res_mon_} 文件
     * @param fileName
     * @param folderName
     * @return
     * @throws Exception
     */
    @Override
    public List<String> getYdDataByFileName(String fileName, String folderName) throws Exception {
        //SftpClientUtil instans = SftpClientUtil.getInstans(SFTPConstants.SFTP_REQ_LOCAL_HOST, SFTPConstants.SFTP_REQ_LOCAL_USERNAME, SFTPConstants.SFTP_REQ_LOCAL_PASSWORD, SFTPConstants.SFTP_DEFAULT_LOCAL_PORT, "UTF-8");
        SftpClientUtil instans = SftpClientUtil.getInstans("192.168.10.1", "test", "test", 220, "UTF-8");
        List<String> list = instans.getYdFilesDateOne(SFTPConstants.REMOTEPATH, folderName, fileName);
        return list;
    }




}
